
<?php
//链接数据库
class Db{
    //属性
    private $host;
    private $root;
    private $password;
    private $database;
    private $port;
    public $conn;//保存数据库链接
    //构造函数的作用初始化属性
    public function __construct($root,$password,$database,$host="localhost",$port="3306")
    {
        $this->host=$host;
        $this->root=$root;
        // $this->host=isset($host)?$host:"localhost";
        // $this->root=isset($root)?$root:"root";
        $this->password=$password;
        $this->database=$database;
        $this->port=$port;
        $this->connectDb();
        // $this->port=isset($port)?$port:$port;
    }
    //链接数据库
    public function connectDb(){
        //创建数据库
       $this->conn= mysqli_connect($this->host,$this->root,$this->password,$this->database);
       //检测数据库是否链接成功
       if($this->conn){
           echo "数据库链接成功";
       }else{
           echo "数据库链接失败".mysqli_error($this->conn);
           return;
       }
    }
    //增加数据
    public function write(){
        $sql="INSERT INTO  user VALUES('2','zhangsan','10')";
        if(mysqli_query($this->conn,$sql)){
        echo "插入成功";
       
    } else{
        echo "插入失败".mysqli_error($this->conn);
    }
}
//删除数据

//修改数据
// public function edit(){
//     $sql="UPDATE user SET name = 'lisi' where id=1";
//     if(mysqli_query($this->conn,$sql)){
//         echo"修改成功";
//     }else{
//         echo "失败".mysqli_error($this->conn);
//     }
// }
public function exec($sql){
    if(mysqli_query($this->conn,$sql)){
        echo"成功";
    }else{
        echo "失败".mysqli_error($this->conn);
    }
}
//查询数据
// public function select($sql){
//     if(mysqli_query($this->conn,$sql)){
//         echo"查询成功";
//     }else{
//         echo"查询失败";
//     }
// }
}
$db=new Db("root","root","test","127.0.0.1");
// $db->write();
// $sql="UPDATE user SET name='lisi' where id=3";
$sql="DELETE FROM user WHERE name='zhangsan'";
$db->exec($sql);
// $db->edit();
// $db->delete();



// var_dump($db);
?>